#include "settingdialog.h"

SettingDialog::SettingDialog(QDialog *parent) : QDialog(parent)
{
    this->setWindowFlags(Qt::WindowTitleHint);
    this->move(100,100);
    this->resize(300,280);
    setFixedSize(this->width(), this->height());
    this->setWindowTitle("设置@Avengers");
    this->setWindowIcon(QIcon(":/point/Point/Settings.jpg"));
    Layout0 = new QGridLayout();
    Box0 = new QGroupBox("边沿颜色",this);
    Box0->setLayout(Layout0);
    Box0->resize(140,105);
    Box0->move(0,0);
    l_av = new QPushButton(this);
    l_av->setText("左侧\n有效");

    r_av = new QPushButton(this);
    r_av->setText("右侧\n有效");

    l_iav = new QPushButton(this);
    l_iav->setText("左侧\n无效");

    r_iav = new QPushButton(this);
    r_iav->setText("右侧\n无效");

    Layout0->addWidget(l_av,0,0,1,2);
    Layout0->addWidget(r_av,0,3,1,2);
    Layout0->addWidget(l_iav,1,0,1,2);
    Layout0->addWidget(r_iav,1,3,1,2);

    bl_av = new QLabel(this);
    Layout0->addWidget(bl_av,0,2,1,1);

    br_av = new QLabel(this);
    Layout0->addWidget(br_av,0,5,1,1);

    bl_iav = new QLabel(this);
    Layout0->addWidget(bl_iav,1,2,1,1);

    br_iav = new QLabel(this);
    Layout0->addWidget(br_iav,1,5,1,1);

    i_lav = new QImage(1,1,QImage::Format_RGB888);
    i_rav = new QImage(1,1,QImage::Format_RGB888);
    i_liav = new QImage(1,1,QImage::Format_RGB888);
    i_riav = new QImage(1,1,QImage::Format_RGB888);
    connect(this,SIGNAL(setColor(int)),this,SLOT(settingColor(int)));
    connect(l_av,SIGNAL(clicked(bool)),this,SLOT(set1()));
    connect(r_av,SIGNAL(clicked(bool)),this,SLOT(set2()));
    connect(l_iav,SIGNAL(clicked(bool)),this,SLOT(set3()));
    connect(r_iav,SIGNAL(clicked(bool)),this,SLOT(set4()));

    yes = new QPushButton(this);
    yes->setText("确认并返回");
    yes->resize(90,20);
    yes->move(160,260);
    connect(yes,SIGNAL(clicked(bool)),this,SLOT(emitChange()));
    connect(yes,SIGNAL(clicked(bool)),this,SLOT(close()));

    no = new QPushButton(this);
    no->setText("取消");
    no->resize(60,20);
    no->move(160,240);
    connect(no,SIGNAL(clicked(bool)),this,SLOT(close()));

    defaultButton = new QPushButton(this);
    defaultButton->setText("默认");
    defaultButton->resize(60,20);
    defaultButton->move(220,240);
    connect(defaultButton,SIGNAL(clicked(bool)),this,SLOT(setDefault()));

    apply = new QPushButton(this);
    apply->setText("应用");
    apply->resize(30,20);
    apply->move(250,260);
    connect(apply,SIGNAL(clicked(bool)),this,SLOT(emitChange()));

    setScale = new QComboBox(this);


    setScale->addItem("160 X 120");
    setScale->addItem(" 80 X 60");

    setScale->move(210,30);
    setScale->resize(80,25);

    scaleLabel = new QLabel(this);
    scaleLabel->setText("图像尺寸");
    scaleLabel->resize(50,30);
    scaleLabel->move(150,30);

    Box1 = new QGroupBox(this);
    setNameLayout = new QGridLayout();
    Box1->setFixedSize(130,170);
    Box1->setLayout(setNameLayout);
    Box1->move(150,70);
    Box1->setTitle("预留变量名");
    for (int i=0;i<5;++i)
    {
        nameEdit0[i] = new QLineEdit(this);
        nameLabel[i] = new QLabel(this);
        nameLabel[i]->setText("预留"+QString::number(i));
        setNameLayout->addWidget(nameLabel[i],i,0,1,1);
        setNameLayout->addWidget(nameEdit0[i],i,1,1,1);
    }

    loadFile = new QPushButton(this);
    loadFile->setText("载入配置");
    loadFile->move(150,0);
    loadFile->resize(70,25);
    connect(loadFile,SIGNAL(clicked(bool)),this,SLOT(load()));

    saveFile = new QPushButton(this);
    saveFile->move(220,0);
    saveFile->resize(80,25);
    saveFile->setText("应用并保存");
    connect(saveFile,SIGNAL(clicked(bool)),this,SLOT(save()));

    //位变量相关设置
    bitBox = new QGroupBox("位变量名称",this);
    bitBox->move(0,110);
    bitLayout = new QGridLayout();
    bitBox->setLayout(bitLayout);
    bitBox->setFixedSize(140,170);
    for (int i=0;i<=3;i++)
    {
        for (int j=0;j<=1;j++)
        {
            bitLabel[i*2+j] = new QLabel(this);
            bitLabel[i*2+j]->setText(QString::number(i*2+j));
            bitLayout->addWidget(bitLabel[i*2+j],i,j*3,1,1);
            bitEdit[i*2+j] = new QLineEdit(this);
            bitLayout->addWidget(bitEdit[i*2+j],i,j*3+1,1,2);
        }
    }
    setScale->setCurrentIndex(1);
}
void SettingDialog::setcolor()
{
//色彩选择
    i_lav->setPixel(0,0,rgb_lav);
    bl_av->setPixmap(QPixmap::fromImage(*i_lav).scaled(50,50));

    i_rav->setPixel(0,0,rgb_rav);
    br_av->setPixmap(QPixmap::fromImage(*i_rav).scaled(50,50));

    i_liav->setPixel(0,0,rgb_liav);
    bl_iav->setPixmap(QPixmap::fromImage(*i_liav).scaled(50,50));

    i_riav->setPixel(0,0,rgb_riav);
    br_iav->setPixmap(QPixmap::fromImage(*i_riav).scaled(50,50));
}
SettingDialog::~SettingDialog()
{
}
void SettingDialog::setDefault()
{
    rgb_lav = qRgb(0,255,0);
    rgb_rav = qRgb(0,0,255);
    rgb_liav = qRgb(255,255,0);
    rgb_riav = qRgb(255,0,0);
    setcolor();
    for(int i=0;i<5;i++)
    {
        currentName[i] = "预留"+QString::number(i);
        nameEdit0[i]->setText(currentName[i]);
    }
    for (int i=0;i<8;i++)
    {
        currentBitName[i] = "位"+QString::number(i);
        bitEdit[i]->setText(currentBitName[i]);
    }
    this->repaint();
}
void SettingDialog::save()
{
    emitChange();
    saveFileName = QFileDialog::
            getSaveFileName(this,"保存配置文件",QCoreApplication::applicationDirPath(),QString("配置文件(*.ini)"));
    if (saveFileName==QString(""))
    {
        QMessageBox::information(this,"注意","配置未保存","确定");
        return;
    }
    QFile f(saveFileName);
    f.open(QFile::WriteOnly);
    QTextStream s;
    s.setDevice(&f);
    //文件存储规范：前四个数字，分别代表lav rav liav riav的rgb；之后五行，自定义变量名；之后8行，位变量
    s << rgb_lav <<" " <<rgb_rav <<" " << rgb_liav <<" " << rgb_riav <<QString("\n\r");
    for (int i=0;i<5;i++)
        s<<currentName[i]<<QString("\n\r");
    for (int i=0;i<8;i++)
        s<<currentBitName[i]<<QString("\n\r");
    f.close();

}
void SettingDialog::load()
{
    loadFileName = "Null";
    loadFileName = QFileDialog::getOpenFileName
            (this,QString("打开配置文件"),QCoreApplication::applicationDirPath(),QString("配置文件(*.ini)"));
    if (loadFileName==QString(""))
    {
        QMessageBox::information(this,"注意","未打开配置文件","确定");
        return;
    }
    QFile f(loadFileName);
    f.open(QFile::ReadOnly);
    QTextStream s;
    s.setDevice(&f);

    s>>rgb_lav;
    s>>rgb_rav;
    s>>rgb_liav;
    s>>rgb_riav;
    setcolor();
    for (int i=0;i<5;i++)
    {
        s>>currentName[i];
        nameEdit0[i]->setText(currentName[i]);
    }
    for (int i=0;i<8;i++)
    {
        s>>currentBitName[i];
        bitEdit[i]->setText(currentBitName[i]);
    }
}
void SettingDialog::loadDefault()
{
    QFile f(loadFileName);
    f.open(QFile::ReadOnly);
    QTextStream s;
    s.setDevice(&f);

    s>>rgb_lav;
    s>>rgb_rav;
    s>>rgb_liav;
    s>>rgb_riav;
    setcolor();
    for (int i=0;i<5;i++)
    {
        s>>currentName[i];
        nameEdit0[i]->setText(currentName[i]);
    }
    for (int i=0;i<8;i++)
    {
        s>>currentBitName[i];
        bitEdit[i]->setText(currentBitName[i]);
    }

}
